package org.mcb.zebra.portal.servlet;

import java.io.IOException;

import javax.portlet.PortletException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.pluto.PortletContainer;
import org.apache.pluto.PortletContainerException;
import org.apache.pluto.PortletWindow;
import org.mcb.zebra.portal.manager.PortalManager;
import org.mcb.zebra.portal.page.PageManager;
import org.mcb.zebra.portal.url.PortalUrl;
import org.mcb.zebra.portal.url.UrlParser;
import org.mcb.zebra.portal.url.PortalUrl.PortletRequestMode;

public class ZebRequestHandler implements RequestHandler {

	@Override
	public void handelRequest(HttpServletRequest req, HttpServletResponse resp,
			ServletContext context) throws ServletException, IOException {
		PortalManager portalManager = (PortalManager) context
				.getAttribute(PortalManager.PORTAL_MANAGER);
		String pageName = (String) req.getSession()
				.getAttribute(PageManager.CURRENT_PAGE);
		if (pageName == null) {
			pageName = portalManager.getDefaultPage();
			req.getSession().setAttribute(PageManager.CURRENT_PAGE, pageName);
		}
		req.setAttribute(PageManager.CURRENT_PAGE, pageName);
		req.setAttribute(PortalManager.LIST_PORTLETS, portalManager.getPageManager().getPage(pageName).getAllPortletWindow());
		PortletContainer container=(PortletContainer) context.getAttribute(PortalManager.PORTLET_CONTAINER);
		
		UrlParser parser =portalManager.getUrlParser();
		PortalUrl portalUrl = parser.getPortalUrl(req);
		if(portalUrl!=null&&portalUrl.getPorletWindowID()!=null){
			PortletWindow window = parser.getPortletWindow(req,portalUrl.getPorletWindowID());
			if(window!=null){
				if(portalUrl.getMode()!=null&&!portalUrl.getMode().equals(window.getPortletMode()))
					portalUrl.setMode(portalUrl.getMode());
				if(portalUrl.getState()!=null&&!portalUrl.getState().equals(window.getWindowState()))
					portalUrl.setState(portalUrl.getState());
				if(PortletRequestMode.ACTION.equals(portalUrl.getPortletRequestMode())){
					try {
						container.doAction(window, req, resp);
					} catch (PortletException e) {
						throw new ServletException(e);
					} catch (PortletContainerException e) {
						throw new ServletException(e);
					}
					
				}else{
					try {
						container.doRender(window, req, resp);
					} catch (PortletException e) {
						throw new ServletException(e);
					} catch (PortletContainerException e) {
						throw new ServletException(e);
					}
				}
				return;
			}
		}else{
			String wid = req.getParameter("porlet-window-id");
			if(wid!=null){
				PortletWindow window = parser.getPortletWindow(req,wid);
				if(window!=null){
					try {
						container.doRender(window, req, resp);
					} catch (PortletException e) {
						throw new ServletException(e);
					} catch (PortletContainerException e) {
						throw new ServletException(e);
					}
					return;
				}
			}
		}
		req.getRequestDispatcher(portalManager.getPageManager().getPage(pageName).getUrl()).include(req, resp);
	}

}
